<template>
  <div class="create-or-edit">
      <el-form :model="formInline" label-width="80px">
        <el-form-item label="角色名称" >
          <el-input v-model="formInline.name"></el-input>
        </el-form-item>
        <el-form-item label="角色编码" >
          <el-input v-model="formInline.code" ></el-input>
        </el-form-item>
        <el-form-item label="角色描述" >
          <el-input v-model="formInline.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="onSubmit"
          >确 定</el-button>
      </div>
  </div>
</template>

<script>
import { saveOrUpdate, getRoleDetail } from '@/services/role'
export default {
  name: 'create-or-edit',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    roleId: {
      type: [Number, String]
    }
  },
  data () {
    return {
      formInline: {
        name: '',
        code: '',
        description: ''
      }
    }
  },
  methods: {
    async loadRole () {
      const { data } = await getRoleDetail(this.roleId)
      console.log(data)
      this.formInline = data.data
    },
    cancle () {
      this.$emit('cancle')
      this.role = {}
    },
    async onSubmit () {
      const { data } = await saveOrUpdate(this.formInline)
      if (data.code === '000000') {
        this.$message.success('操作成功')
        // 关闭弹框，
        this.$emit('success')
        this.role = {}
      }
    }
  },
  created () {
    if (this.isEdit) {
      this.loadRole()
    }
  }
};
</script>

<style>
.dialog-footer{
    text-align: right;
}
</style>
